package utils

import (
	"fmt"
	"strings"
)

func GetUserAkCacheKey(userId string) string {
	return fmt.Sprintf("ak-%s", userId)
}

func GetUserNamespace(userId string) string {
	return NormalizeUnderline("user-" + userId)
}

func GetUserPVCName(userId string) string {
	return fmt.Sprintf("user-%s-pvc", userId)
}

func GetUserPVName(userId string) string {
	return fmt.Sprintf("user-%s-pv", userId)
}

func BashRunCommand(shell string) []string {
	return []string{"bash", "-c",
		shell,
	}
}

func GetEventsHubName(userId, appId string) string {
	return fmt.Sprintf("eventsHub-%s-%s", userId, appId)
}

func GetStreamRedisAddr(appId string) string {
	return fmt.Sprintf("app-%s-redis:6379", appId)
}

func GetCommonNodeServiceName(appId, nodeId, userId string) string {
	return NormalizeUnderline(fmt.Sprintf("app-%s-%s.%s", appId, nodeId, GetUserNamespace(userId)))
}

func NormalizeUnderline(s string) string {
	return strings.ReplaceAll(s, "_", "-")
}

func GetAppCsName(id string) string {
	if strings.Contains(id, ".") {
		arr := strings.Split(id, ".")
		return getAppNodeCsName(arr[0], arr[1])
	}

	if strings.Contains(id, "_") {
		return "cron-" + strings.ReplaceAll(id, "_", "-")
	}

	return "app-" + strings.ReplaceAll(id, "_", "-")
}

func getAppNodeCsName(id string, nodeId string) string {
	return GetAppCsName(id) + "-" + nodeId
}
